﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.Common;
namespace SdmxDiff
{
    public class CodelistDiff
    {
        private ReportDiff report;
        private CodeListType cltNew;
        private CodeListType cltOld;


        public CodelistDiff(ReportDiff rpt, CodeListType cOld, CodeListType cNew)
        {
            report = rpt;
            cltOld = cOld;
            cltNew = cNew;
        }

        public void Diff()
        {
            // The artefact Name
            MultilangDiff mld = new MultilangDiff(cltOld.Name, cltNew.Name);
            if (string.IsNullOrEmpty(mld.DiffComment) == false)
                report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Artefact", "Name", 
                    mld.DiffComment, cltNew.Name, cltOld.Name });

            // The artefact Description
            mld = new MultilangDiff(cltOld.Description, cltNew.Description);
            if (string.IsNullOrEmpty(mld.DiffComment) == false)
                report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Artefact", "Description", 
                    mld.DiffComment, cltNew.Description, cltOld.Description });
            
            // The artefact annotations
            AnnotationDiff and = new AnnotationDiff(cltNew, "Artefact", cltOld.AnnotationList, cltNew.AnnotationList);
            and.Diff(report);
            
            // the codes
            List<CodeType> listNewCodes = new List<CodeType>();
            List<CodeType> listRemovedCodes = new List<CodeType>();

            foreach (CodeType c in cltNew.CodeList)
            {
                if (cltOld.CodeList.LocateItem(c.Id) == null)
                    listNewCodes.Add(c);
            }

            foreach (CodeType c in cltOld.CodeList)
            {
                if (cltNew.CodeList.LocateItem(c.Id) == null)
                    listRemovedCodes.Add(c);
            }
            foreach (CodeType c in listNewCodes)
                report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Code " + c.Id, "New", c.ToString() });
            foreach (CodeType c in listRemovedCodes)
                report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Code " + c.Id, "Removed", c.ToString() });

            foreach (CodeType c in cltNew.CodeList)
            {
                CodeType cOld = cltOld.CodeList.LocateItem(c.Id) as CodeType;
                if (cOld != null)
                {
                    mld = new MultilangDiff(cOld.Name, c.Name);
                    if (string.IsNullOrEmpty(mld.DiffComment) == false)
                        report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Code " + c.Id, "Name", 
                            mld.DiffComment, c.Name, cOld.Name });

                    mld = new MultilangDiff(cOld.Description, c.Description);
                    if (string.IsNullOrEmpty(mld.DiffComment) == false)
                        report.ReportTable.Rows.Add(new object[] { cltNew.ArType.ToString(), cltNew.FullIdent, "Code " + c.Id, "Description", 
                            mld.DiffComment, c.Description, cOld.Description });

                    and = new AnnotationDiff(cltNew, "Code " + c.Id, cOld.AnnotationList, c.AnnotationList);
                    and.Diff(report);                
                }
            }
        }
    }


}
